#include <QtGui/QMouseEvent>
#include "NGLWidget.h"
#include <cstdio>

NGLWidget::NGLWidget(QWidget *parent) : QGLWidget(parent) {
    setMouseTracking(true);
}

void NGLWidget::initializeGL() {
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

void NGLWidget::resizeGL(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
	//gluOrtho2D(0,w,0,h);
	//glOrtho(0, w, 0, h, -1, 10000);
	//glFrustum(0, w, 0, h, -1, 10000);
	gluPerspective(60, (double) w / (double) h, 1.0, 10000.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
	gluLookAt(0.0f, 0.0f, 100.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
}

void NGLWidget::paintGL() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	float* verts = new float[9];

	verts[0] = 10.0f;
	verts[1] = 0.0f;
	verts[2] = 0.0f;

	verts[3] = -10.0f;
	verts[4] = 0.0f;
	verts[5] = 0.0f;

	verts[6] = 0.0f;
	verts[7] = 20.0f;
	verts[8] = 0.0f;

	unsigned int* tris = new unsigned int[3];
	tris[0] = 2;
	tris[1] = 1;
	tris[2] = 0;

	float* cols = new float[9];
	cols[0] = 1.0f;
	cols[1] = 0.0f;
	cols[2] = 0.0f;

	cols[3] = 0.0f;
	cols[4] = 1.0f;
	cols[5] = 0.0f;

	cols[6] = 0.0f;
	cols[7] = 0.0f;
	cols[8] = 1.0f;

	glEnableClientState(GL_VERTEX_ARRAY);
	glVertexPointer(3,GL_FLOAT,0,verts);

	glEnableClientState(GL_COLOR_ARRAY);
	glColorPointer(3,GL_FLOAT,0,cols);

	glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, tris);

	glDisableClientState(GL_COLOR_ARRAY);

	glDisableClientState(GL_VERTEX_ARRAY);

	delete verts;
	delete tris;
	delete cols;
}

void NGLWidget::mousePressEvent(QMouseEvent *event) {
	printf("You did it!");
}
void NGLWidget::mouseMoveEvent(QMouseEvent *event) {
    printf("%d, %d\n", event->x(), event->y());
}

void NGLWidget::keyPressEvent(QKeyEvent* event) {
    switch(event->key()) {
    case Qt::Key_Escape:
        close();
        break;
    default:
        event->ignore();
        break;
    }
}